package com.giscloud.amp.controller.auth;

import com.giscloud.amp.controller.BaseController;
import com.giscloud.amp.entity.Area;
import com.giscloud.amp.entity.dto.AmpGetDataMeansDTO;
import com.giscloud.amp.entity.dto.AmpStaffDataDTO;
import com.giscloud.amp.entity.regis.dto.AreaDTO;
import com.giscloud.amp.service.auth.AmpStaffDataService;
import com.giscloud.amp.service.regis.AreaService;
import com.giscloud.commons.model.Result;
import com.giscloud.token.utils.TokenUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @Author:gxd
 * @Description:
 * @Date: 17:24 2018/2/24
 * @Modified By:
 */
@SuppressWarnings("SpringJavaAutowiringInspection") //加这个注解让IDE 不报: Could not autowire
@RestController
public class AmpStaffDataController extends BaseController {
    @Autowired
    private AmpStaffDataService ampStaffDataRemoteClient;

    @Autowired
    private AreaService areaRemoteClient;
    @Autowired
    private TokenUserUtil tokenUserUtil;
    /**
     * 批量启用数据权限
     * @return
     */
    @RequestMapping("/amp/ampstaffdata/editDataAuth")
    public Result editDataAuth(@RequestBody AmpGetDataMeansDTO ampGetDataMeansDTO) {
        return ampStaffDataRemoteClient.editDataAuth(ampGetDataMeansDTO);
    }

    /**
     * 单独启用禁用数据权限
     * @param ampGetDataMeansDTO
     * @return
     */
    @RequestMapping("/amp/ampstaffdata/enableDataAuth")
    public Result enableDataAuth(@RequestBody AmpGetDataMeansDTO ampGetDataMeansDTO) {
        return ampStaffDataRemoteClient.enableDataAuth(ampGetDataMeansDTO);
    }

    @RequestMapping("/amp/ampstaffdata/getObjectDataIds")
    public List<Map> getObjectDataIds(@RequestBody AmpGetDataMeansDTO ampGetDataMeansDTO){
        return ampStaffDataRemoteClient.getObjectDataIds(ampGetDataMeansDTO);
    }

    @RequestMapping("/amp/ampstaffdata/findDataList")
    public List findAreaList(@RequestBody AmpGetDataMeansDTO ampGetDataMeansDTO){
            AreaDTO areaDTO = new AreaDTO();
            areaDTO.setAreaLevel(Math.toIntExact(ampGetDataMeansDTO.getDataType()));
            areaDTO.setName(ampGetDataMeansDTO.getName());
            areaDTO.setDataType(ampGetDataMeansDTO.getData2Type());
            areaDTO.setAreaId(ampGetDataMeansDTO.getAreaId());
            areaDTO.setParentArea(ampGetDataMeansDTO.getParentId());
            return areaRemoteClient.findAreaList(areaDTO);
    }

    @RequestMapping("/amp/ampstaffdata/findDataListByStatus")
    public List findDataListByStatus(@RequestBody AmpGetDataMeansDTO ampGetDataMeansDTO){
        AreaDTO areaDTO = new AreaDTO();
        areaDTO.setAreaLevel(Math.toIntExact(ampGetDataMeansDTO.getDataType()));
        areaDTO.setName(ampGetDataMeansDTO.getName());
        areaDTO.setDataType(ampGetDataMeansDTO.getData2Type());
        areaDTO.setAreaId(ampGetDataMeansDTO.getAreaId());
        areaDTO.setParentArea(ampGetDataMeansDTO.getParentId());
        //获取启用的
        if("1".equals(ampGetDataMeansDTO.getStatusCd())){
            List<Map> objectDataIds = ampStaffDataRemoteClient.getObjectDataIds(ampGetDataMeansDTO);
            List<Area> areaList = areaRemoteClient.findAreaList(areaDTO);
            return removeAllForEnable(objectDataIds,areaList);
        }else if("2".equals(ampGetDataMeansDTO.getStatusCd())){
            //未启用
            List<Area> areaList = areaRemoteClient.findAreaList(areaDTO);
            List<Map> objectDataIds = ampStaffDataRemoteClient.getObjectDataIds(ampGetDataMeansDTO);
            return removeAll(areaList,objectDataIds);
        }
        //全部
        return areaRemoteClient.findAreaList(areaDTO);
    }
    private  List<Map> removeAllForEnable(List<Map> left, List<Area> right){
        if (left == null){
            return null;
        }
        if (right == null){
            return left;
        }
        //使用LinkedList方便插入和删除
        List<Map> res = new LinkedList<>(left);
        Set<String> set = new HashSet<>();
        //将right id存放到set
        for (Area area : right) {
            set.add(area.getAreaId().toString());
        }
        //迭代器遍历listA
        Iterator<Map> iter = res.iterator();
        while(iter.hasNext()){
            Map next = iter.next();
            //如果set中包含id则remove
            if(!set.contains(next.get("areaId").toString())){
                iter.remove();
            }
        }
        return res;
    }
    private  List<Area> removeAll(List<Area> left, List<Map> right){
        if (left == null){
            return null;
        }
        if (right == null){
            return left;
        }
        //使用LinkedList方便插入和删除
        List<Area> res = new LinkedList<>(left);
        Set<String> set = new HashSet<>();
        //将right id存放到set
        for (Map map : right) {
            set.add(map.get("areaId").toString());
        }
        //迭代器遍历listA
        Iterator<Area> iter = res.iterator();
        while(iter.hasNext()){
            Area item = iter.next();
            //如果set中包含id则remove
            if(set.contains(item.getAreaId().toString())){
                iter.remove();
            }
        }
        return res;
    }

    @RequestMapping("/amp/findCityList")
    public List getCity(){
        AmpStaffDataDTO ampStaffDataDTO = new AmpStaffDataDTO();
        ampStaffDataDTO.setSystemId(1L);
        List<Long> roles = new ArrayList<>();
        for(String str : getToken().getRolesId()) {
            roles.add(Long.parseLong(str));
        }
        ampStaffDataDTO.setAreaLevel(1L);
        ampStaffDataDTO.setRolesId(roles);
        ampStaffDataDTO.setTableName("comm.area");
        ampStaffDataDTO.setStaffId(tokenUserUtil.getUserId());
        List<Map> cityList = ampStaffDataRemoteClient.findCityList(ampStaffDataDTO);
        return cityList;
    }

    @RequestMapping("/amp/listArea")
    public Result listArea(@RequestBody Long id){
        if(id == 0L){
            AreaDTO areaDTO = new AreaDTO();
            areaDTO.setAreaLevel(1);
            List<Area> areas = areaRemoteClient.listArea(areaDTO);
            return Result.success(areas);
        }else{
            AreaDTO areaDTO = new AreaDTO();
            areaDTO.setAreaId(id);
            List<Area> areas = areaRemoteClient.listArea(areaDTO);
            return Result.success(areas);
        }
    }
}
